package com.guobox.movies.utils;

import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;

import java.util.Timer;

/**
 * Created by xuxinlei on 2017/7/14.
 */

public abstract class ResetTimer {
    private long delayTime = 0;

    private int MSG = 1;

    public ResetTimer(long delayTime) {
        this.delayTime = delayTime;
    }

    public abstract void onTimerDown();
    public synchronized final void start() {
        handler.sendEmptyMessageDelayed(MSG,delayTime);
    }

    public synchronized final void reset() {
        handler.removeMessages(MSG);
        handler.sendEmptyMessageDelayed(MSG, delayTime);
    }

    public synchronized final void cancel() {
        handler.removeMessages(MSG);
    }
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            synchronized (ResetTimer.this) {
                onTimerDown();
            }
        }
    };


}
